package com.ruoyi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.bean.*;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.dto.ParkSpaceDTO;
import com.ruoyi.entity.*;
import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder;
import com.ruoyi.mapper.ParkSpaceMapper;
import com.sxftech.utils.CopyBeanUtils;
import com.sxftech.utils.exception.WarnException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@DataSource(value = DataSourceType.SLAVE)  //切换数据源
@Transactional
public class ParkService extends ServiceImpl<ParkSpaceMapper, ParkSpace> {
    @Autowired
    private ParkSpaceMapper parkSpaceMapper;
    @Autowired
    private BizBusinessSyncService bizBusinessSyncService;
    @Autowired
    private BizBusinessPktToCloudService bizBusinessPktToCloudService;
    @Autowired
    private SxfBusinessPktToCloudService sxfBusinessPktToCloudService;
    @Autowired
    private BizParkingSpaceService bizParkingSpaceService;
    @Autowired
    private SxfPriceParkingSpaceService sxfPriceParkingSpaceService;
    @Autowired
    private BizPriceParkingSpaceService bizPriceParkingSpaceService;
    @Autowired
    private SxfParkingSpaceService sxfParkingSpaceService;

    //查询所有的停车场
    public List<BizParkingLotFrom> listAll(){
        return parkSpaceMapper.listAll();
    }

    //处理同步
    public AjaxResult deleteParkSys(String id) throws Exception{

        BizBusinessSyncFrom bizBusinessSyncFrom = bizBusinessSyncService.findById(id);
        if(bizBusinessSyncFrom !=null) {
            if (bizBusinessSyncFrom.getOperStatus() == 0 && bizBusinessSyncFrom.getRemark().equalsIgnoreCase("数据异常，原车位业务不存在")) {
                List<BizBusinessPktToCloud> bizBusinessPktToClouds = bizBusinessPktToCloudService.oneList(bizBusinessSyncFrom.getBizId(), bizBusinessSyncFrom.getPlCode(),bizBusinessSyncFrom.getPlateNumber());
                if (bizBusinessPktToClouds == null) {
                    throw new WarnException("找不到该条本地端和云端业务关系数据");
                }
                BizBusinessPktToCloud bizBusinessPktToCloud  = bizBusinessPktToClouds.get(0);
                SxfBusinessPktToCloudFrom sxfBusinessPktToCloudFrom = new SxfBusinessPktToCloudFrom();
                CopyBeanUtils.copyNonNullPropertites(bizBusinessPktToCloud,sxfBusinessPktToCloudFrom);
                sxfBusinessPktToCloudFrom.setToId(sxfBusinessPktToCloudFrom.getId());


                sxfBusinessPktToCloudService.create(sxfBusinessPktToCloudFrom); //保留记录到我们的数据库
                bizBusinessPktToCloudService.removeById(bizBusinessPktToCloud.getId()); //删除分享停车数据库记录
            }else{
                throw new WarnException("不符合删除条件");
            }
        }
        return AjaxResult.success("删除成功！可重新上传");
    }


    //同步成功后没有业务显示的处理
    public AjaxResult parkSysPrice(String id) throws Exception{
        BizBusinessSyncFrom bizBusinessSyncFrom = bizBusinessSyncService.findById(id);
        if(bizBusinessSyncFrom !=null) {
            List<BizBusinessPktToCloud> bizBusinessPktToClouds = bizBusinessPktToCloudService.oneList(bizBusinessSyncFrom.getBizId(), bizBusinessSyncFrom.getPlCode(), bizBusinessSyncFrom.getPlateNumber());
            if (bizBusinessPktToClouds == null) {
                throw new WarnException("找不到该条本地端和云端业务关系数据");
            }
            BizBusinessPktToCloud bizBusinessPktToCloud  = bizBusinessPktToClouds.get(0);
            SxfBusinessPktToCloudFrom sxfBusinessPktToCloudFrom = new SxfBusinessPktToCloudFrom();
            CopyBeanUtils.copyNonNullPropertites(bizBusinessPktToCloud,sxfBusinessPktToCloudFrom);
            sxfBusinessPktToCloudFrom.setToId(sxfBusinessPktToCloudFrom.getId());
            sxfBusinessPktToCloudService.create(sxfBusinessPktToCloudFrom); //保留记录到我们的数据库
            bizBusinessPktToCloudService.removeById(bizBusinessPktToCloud.getId()); //删除分享停车数据库记录


            List<BizParkingSpace> bizParkingSpaces =  bizParkingSpaceService.oneList(bizBusinessSyncFrom.getPlCode(),bizBusinessSyncFrom.getSpaceCode());
            if(bizParkingSpaces !=null){
                List<BizPriceParkingSpace> bizPriceParkingSpaces = bizPriceParkingSpaceService.oneList(bizParkingSpaces.get(0).getPlCode(),bizParkingSpaces.get(0).getId());
                if(bizPriceParkingSpaces !=null){
                    for(BizPriceParkingSpace bizPriceParkingSpace:bizPriceParkingSpaces){
                        SxfPriceParkingSpaceFrom sxfPriceParkingSpaceFrom = new SxfPriceParkingSpaceFrom();
                        CopyBeanUtils.copyNonNullPropertites(bizPriceParkingSpace,sxfPriceParkingSpaceFrom);
                        sxfPriceParkingSpaceFrom.setPriceId(bizPriceParkingSpace.getId());
                        sxfPriceParkingSpaceService.create(sxfPriceParkingSpaceFrom); //将价格体系与车位关系新增到自己的数据库

                        bizPriceParkingSpaceService.removeById(bizPriceParkingSpace.getId()); //删除分享停车价格体系与车位关系数据
                    }
                }
            }
        }
        return AjaxResult.success("删除成功！可重新上传");
    }

    public AjaxResult deleteParkingSpace(String id) throws WarnException {
        BizParkingSpaceFrom bizParkingSpaceFrom = bizParkingSpaceService.findById(id);
        if(bizParkingSpaceFrom !=null){
            List<BizParkingSpace> list = bizParkingSpaceService.list(new QueryWrapper<BizParkingSpace>().lambda().eq(BizParkingSpace::getPlCode,bizParkingSpaceFrom.getPlCode())
            .eq(BizParkingSpace::getSpaceCode,bizParkingSpaceFrom.getSpaceCode()));
            if(list.size()>1){
                if(list.get(0).getId().compareTo(list.get(1).getId()) < 0){
                    BizParkingSpace bizParkingSpace = list.get(1);
                    SxfParkingSpace sxfParkingSpace = new SxfParkingSpace();
                    CopyBeanUtils.copyNonNullPropertites(bizParkingSpace,sxfParkingSpace);
                    sxfParkingSpace.setSpaceId(bizParkingSpace.getId());
                    sxfParkingSpaceService.save(sxfParkingSpace);
                    bizParkingSpaceService.removeById(bizParkingSpace.getId());
                }else{
                    throw new WarnException("不符合删除条件");
                }
            }
        }
        return null;
    }

    /*public AjaxResult deleteParkingSpaces(String id) throws WarnException{
        BizParkingSpaceFrom bizParkingSpaceFrom = bizParkingSpaceService.findById(id);
        if(bizParkingSpaceFrom !=null){
            List<BizParkingSpace> list = bizParkingSpaceService.list(new QueryWrapper<BizParkingSpace>().lambda().eq(BizParkingSpace::getPlCode,bizParkingSpaceFrom.getPlCode())
                    .eq(BizParkingSpace::getSpaceCode,bizParkingSpaceFrom.getSpaceCode()));
            if(list.size()>1){
                if(list.get(0).getId().compareTo(list.get(1).getId()) < 0){
                    BizParkingSpace bizParkingSpace = list.get(1);
                    SxfParkingSpace sxfParkingSpace = new SxfParkingSpace();
                    CopyBeanUtils.copyNonNullPropertites(bizParkingSpace,sxfParkingSpace);
                    sxfParkingSpace.setSpaceId(bizParkingSpace.getId());
                    sxfParkingSpaceService.save(sxfParkingSpace);
                    bizParkingSpaceService.removeById(bizParkingSpace.getId());

                    List<BizPriceParkingSpace> bizPriceParkingSpaces = bizPriceParkingSpaceService.list(new QueryWrapper<BizPriceParkingSpace>().lambda().eq(BizPriceParkingSpace::getPlCode,list.get(0).getPlCode())
                                                    .eq(BizPriceParkingSpace::getSpaceId,list.get(0).getId()).eq(BizPriceParkingSpace::getIsDeleted,'N'));

                    if(bizPriceParkingSpaces!=null){
                        List<BizPriceParkingSpace> bizPriceParkingSpaces1 = new ArrayList<>();
                        List<BizPriceParkingSpace> bizPriceParkingSpaces2 = new ArrayList<>();
                        for (int i=0;i<bizPriceParkingSpaces.size();i++){
                            if(bizPriceParkingSpaces.get(i).getValidateTime()!=null){
                                bizPriceParkingSpaces1.add(bizPriceParkingSpaces.get(i));
                            }else{
                                bizPriceParkingSpaces2.add(bizPriceParkingSpaces.get(i));
                            }

                            *//*for (int j=0;j<bizPriceParkingSpaces.size();j++){
                                if(bizPriceParkingSpaces.get(j).getValidateTime() !=null){

                                }
                            }*//*
                        }
                        if(bizPriceParkingSpaces1 !=null && bizPriceParkingSpaces2 !=null){
                            for (int k=0;k<bizPriceParkingSpaces1.size();k++){
                                for(int h=0;h<bizPriceParkingSpaces1.size();h++){
                                    if (bizPriceParkingSpaces1.get(k).getModifyTime().compareTo(bizPriceParkingSpaces1.get(h).getModifyTime())<0){
                                        bizPriceParkingSpaces1.remove(k);
                                        h--;
                                    }else if(bizPriceParkingSpaces1.get(k).getModifyTime().compareTo(bizPriceParkingSpaces1.get(h).getModifyTime())==0
                                    &&bizPriceParkingSpaces1.get(k).getId().compareTo(bizPriceParkingSpaces1.get(h).getId())<0){
                                        bizPriceParkingSpaces1.remove(h);
                                        h--;
                                    }else{
                                        bizPriceParkingSpaces1.remove(h);
                                        h--;
                                    }
                                }
                            }

                        }
                    }

                }else{
                    throw new WarnException("不符合删除条件");
                }
            }
        }
        return null;
    }*/
}
